# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: fffffff_teacher.py
@time: 2021/9/4 14:02
'''
from fontTools.ttLib import TTFont
from os import path, makedirs, getcwd, remove
import re, requests, time
import numpy as np
# &#xee97;&#xed7a;&#xe1de;.&#xe4f6;万 137.8
maoyan_dict = {
    'uniE436': '9',
    'uniEF6A': '4',
    'uniE124': '5',
    'uniF58C': '8',
    'uniF854': '0',
    'uniE917': '1',
    'uniE603': '2',
    'uniF424': '3',
    'uniE20F': '7',
    'uniEFDD': '6'
}
write_path = 'fonts'
cur_path = 'cache'

# 获取字体坐标
# get font axis
def getAxis(font):
    uni_list = font.getGlyphOrder()[2:]
    font_axis = []
    for uni in uni_list:
        axis = []
        for i in font['glyf'][uni].coordinates:
            axis.append(i)
        font_axis.append(axis)
    return font_axis

# 获取对比字体文件   第一份文件
base_font = TTFont('fonts/xl.woff')
uni_base_list = base_font.getGlyphOrder()[2:]
base_axis = getAxis(base_font)
base_font = None

# 使用该函数获取当前页面动态字体
# get current font by use this function
def getFont(response):
    font_url = 'http:' + re.search(r"url\('(.*\.woff)'\)", response).group(1)
    print('download:\t' + font_url)
    font_file = requests.get(font_url).content
    writeFont(font_file)
    return parseFont()

# save .woff
def writeFont(font_file):
    global cur_path
    if not path.exists(write_path):
        makedirs(write_path)
    cur_path = path.join(write_path, str(time.time()) + '.woff')
    with open(cur_path, 'wb') as f:
        f.write(font_file)

# 获取当前页面动态字体的字典
def parseFont():
    print('open:\t' + cur_path)
    cur_font = TTFont(cur_path)
    uni_list = cur_font.getGlyphOrder()[2:]
    cur_axis = getAxis(cur_font)
    font_dict = {}
    for i in range(len(uni_list)):
        min_avg, uni = 99999, None
        for j in range(len(uni_base_list)):
            avg = compare_axis(cur_axis[i], base_axis[j])
            if avg < min_avg:
                min_avg = avg
                uni = uni_base_list[j]
                # &#xee97  1
                print(uni)

        font_dict['&#x' + uni_list[i][3:].lower() +';'] = maoyan_dict[uni]
    remove(cur_path)
    return font_dict

# 使用欧式距离计算  坐标计算
# 数据分析里面去了   numpy  处理矩阵的
# use Euclidean Distance to compare
def compare_axis(axis1, axis2):
    if len(axis1) < len(axis2):
        axis1.extend([0,0] for _ in range(len(axis2) - len(axis1)))
    elif len(axis2) < len(axis1):
        axis2.extend([0,0] for _ in range(len(axis1) - len(axis2)))
    axis1 = np.array(axis1)
    axis2 = np.array(axis2)
    return np.sqrt(np.sum(np.square(axis1-axis2)))

def test():
    global cur_path
    cur_path = './cache/1628256786.1324918.woff'
    print(parseFont())

def xl():
    fonts = TTFont('./fonts/xl.woff')
    fonts.saveXML('xl.xml')

if __name__ == "__main__":
    test()
    # TTFont('cache\\1579398826.1402967.woff').saveXML('b1.xml')
    # TTFont('font\\maoyan.woff').saveXML('b2.xml')
    # xl()